import time
import requests


def sync_http(url, count):
    ''' 同步方式发送http 请求'''
    for i in range(count):
        resp = requests.get(url)
        print(resp.text)

def async_http(url, count):
    ''' 协程方式发送http 请求'''
    import gevent
    from gevent import socket
    from gevent import monkey; monkey.patch_socket()
    
    def func(url):
        result = requests.get(url)
        print(result.text)
        return result
        
    
    jobs = [gevent.spawn(func, url) for i in range(count)]
    gevent.joinall(jobs, timeout=1)
    
    print([job.value for job in jobs])


if __name__ == '__main__':
    '''  测试 同步和异步 两种方式的效率 '''

    url = 'https://www.baidu.com/'
    count = 20

    start = time.time()
    sync_http(url, count)
    # async_http(url, count)

    print('use time: ', time.time() - start)
